package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student_Information {
    ResultSet result=null;
    private String id;
    private String name;
    private String classes;
    private String course;
    private JFrame userview;
    private Connection connection;
    private int score;
    public JFrame studentView=new JFrame();
    private JPanel panel = new JPanel(new GridLayout());
    JTextField textField_stu=new JTextField();
    JTextField textField_class=new JTextField();
    JTextField textField_account=new JTextField();
    JTextField textField_course=new JTextField();
    JTextField fun_course=new JTextField();
    JButton submit=new JButton("提交");
    JButton Return=new JButton("返回");
    public Student_Information(Connection connection,String id,JFrame userview){
        this.connection=connection;
        this.userview=userview;
        this.id=id;
        init();
    }
    void init(){
        studentView=new JFrame();
        studentView.setTitle("学生信息查询");
        studentView.setSize(500,500);
        studentView.setLocationRelativeTo(null);
        studentView.setVisible(true);
        GridLayout layout=new GridLayout(6,2);
        JPanel panel=new JPanel(layout);//设置为layout布局
        JLabel L_stu=new JLabel("名字");
        JLabel L_class=new JLabel("班级");
        JLabel L_account=new JLabel("账户");
        JLabel L_course=new JLabel("已选课程");
        JLabel L_fun=new JLabel("所选课程成绩");
        L_fun.setFont(new Font("",Font.BOLD,22));


        fun_course.setFont(new Font("",Font.BOLD,22));
        textField_account.setEditable(false);
        textField_course.setEditable(false);
        fun_course.setEditable(false);
        L_stu.setFont(new Font("",Font.BOLD,22));
        L_class.setFont(new Font("",Font.BOLD,22));
        L_account.setFont(new Font("",Font.BOLD,22));
        L_course.setFont(new Font("",Font.BOLD,22));
        textField_course.setFont(new Font("",Font.BOLD,22));
        textField_class.setFont(new Font("",Font.BOLD,22));
        textField_stu.setFont(new Font("",Font.BOLD,22));
        textField_account.setFont(new Font("",Font.BOLD,22));
        submit.setFont(new Font("",Font.BOLD,22));
        Return.setFont(new Font("",Font.BOLD,22));
        giveFun();
        panel.add(L_account);
        panel.add(textField_account);
        panel.add(L_class);
        panel.add(textField_class);
        panel.add(L_stu);
        panel.add(textField_stu);
        panel.add(L_course);
        panel.add(textField_course);
        panel.add(L_fun);
        panel.add(fun_course);
        panel.add(submit);
        panel.add(Return);
        studentView.add(panel);
        studentView.setVisible(true);
        getInformation();
        function();
    }
    public void getInformation(){
        try {
            PreparedStatement stmt=null;
            String sql = "select * from 学生基本信息表 where id= ?  ";
            System.out.println(id);
            stmt = connection.prepareStatement(sql);
            stmt.setString(1, id);
            result = stmt.executeQuery();
            if (result.next()) {
                textField_account.setText(result.getString(1));
                textField_class.setText(result.getString(3));
                textField_stu.setText(result.getString(2));
                textField_course.setText(result.getString(4));
                course=result.getString(4);
                System.out.println("!"+course);
            }
            giveFun();
        }catch (Exception e){
        }
    }
    public void function(){
        submit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    id=textField_account.getText();
                    name=textField_stu.getText();
                    classes=textField_class.getText();
                    course=textField_course.getText();
                    String sql = "update  学生基本信息表 set name=?,class=? where id=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1,name);
                    pstmt.setString(2,classes);
                    pstmt.setString(3,id);
                    giveFun();
                    int count = pstmt.executeUpdate();
                    if(count==1){
                        JOptionPane.showMessageDialog(
                                studentView,
                                "提交成功",
                                "Yes",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }catch(Exception exception){

                }
            }
        });
        Return.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userview.setVisible(true);
                studentView.dispose();
            }
        });

    }
    public void giveFun(){
        try{
            course=textField_course.getText();

            String sql = " select score from score where id=? and course=? ";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, course);
            ResultSet resultset = pstmt.executeQuery();
            if (resultset.next())
                score = resultset.getInt(1);
            fun_course.setText( String.valueOf(score));
        }catch (Exception e){
            score=0;
            fun_course.setText( String.valueOf(score));

        }

    }
}